// 路由扁平化
export const flattenRoutes = (params) => {
  return params.reduce((prev, curr) => {
    prev.push(curr);
    if (curr.children && curr.children.length > 0) {
      return prev.concat(flattenRoutes(curr.children));
    }
    return prev;
  }, []);
};

// 二级路由
export const childrenRoutes = (params) => {
  return params
    .map((item) => {
      if (item.children && item.children.length > 0) {
        return item.children.reduce((prev, curr) => {
          prev.push(curr);
          if (curr.children && curr.children.length > 0) {
            return prev.concat(childrenRoutes([curr]));
          }
          return prev;
        }, []);
      }
    })
    .flat();
};

// menu菜单路由
export const menuRouter = (params) => {
  return params.map((item) => {
    return {
      label: item.title,
      key: item.path,
      icon: item.icon,
      children: item.children && item.children.length > 0 ? menuRouter(item.children) : false,
    };
  });
};

// 面包屑路由
export const breadcrumbList = (params) => {
  return params
    .split('/')
    .slice(1)
    .map((item) => {
      return '/' + item;
    })
    .reduce((prev, curr) => {
      console.log(prev);
      if (prev[prev.length - 1]) {
        prev.push(prev[prev.length - 1] + curr);
      } else {
        prev.push(curr);
      }
      return prev;
    }, []);
};
